package com.lgy.rabbit.listener;


import com.lgy.rabbit.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

@Slf4j
@Component
public class UserRabbitListener {

    /**
     *
     * @param message
     * @throws IOException
     */
    @RabbitListener(queues = RabbitConfig.SEND_MSM_FANOUT)
    public void orderMessage(Message message) throws IOException {
        try {
            String data = new String(message.getBody());
            // 业务处理逻辑
            log.debug("处理消息: {}", data);

            // 手动确认消息
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 拒绝消息并重新入队
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

    @RabbitListener(queues = RabbitConfig.SEND_MSM_QUEUE)
    public void demoMessage(Message message, Channel channel) throws IOException {
        try {
            String data = new String(message.getBody());
            // 业务处理逻辑
            log.debug("处理消息: {}", data);

            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            // 拒绝消息并重新入队
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }


    @RabbitListener(queues = RabbitConfig.EMAIL_TOPIC_QUEUE)
    public void emailMessage(Message message, Channel channel) throws IOException {
        try {
            String data = new String(message.getBody());
            // 业务处理逻辑
            log.debug("处理消息: {}", data);
            int i = 1 / 0;
            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("处理消息异常: {}", e.getMessage());
            // 拒绝消息并进度DLQ
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            //throw e;
        }
    }

    /*@RabbitListener(queues = RabbitConfig.USER_MESSAGE_NORMAL_QUEUE)
    public void userMessage(Message message, Channel channel) throws IOException {
        try {
            String data = new String(message.getBody());
            // 业务处理逻辑
            log.debug("处理消息: {}", data);
            int i = 1 / 0;
            //手动确认消息
            //channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("处理消息异常: {}", e.getMessage());
            // 拒绝消息并进度DLQ
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            //throw e;
        }
    }*/

    @RabbitListener(queues = RabbitConfig.USER_MESSAGE_DLX_QUEUE)
    public void userDlxMessage(Message message, Channel channel) throws IOException {
        try {
            String data = new String(message.getBody());
            // 业务处理逻辑
            log.debug("执行时间：{}  死信队列处理消息: {}",new Date(), data);
            // 手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("死信队列处理消息异常: {}", e.getMessage());
            // 拒绝消息并进度DLQ
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
            throw e;
        }
    }
}
